package org.gzc.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 验证码
 */
@Controller
public class VerificationCodeController {

    private char[] code = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'n', 'm', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
    private static final int WIDTH = 80; // 验证码宽度
    private static final int HEIGHT = 30; // 验证码高度
    private static final int LENGTH = 4; // 字符长度

    @RequestMapping("/verificationCode")
    public void verificationCode(HttpServletResponse response, HttpSession session) {
        // 禁止缓存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Expires", "0"); // 缓存0秒后过期
        // 设置响应的MIME类型
        response.setContentType("image/jpeg");
        // 绘制验证码
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        // 获得画笔
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        // 填充背景
        g2d.setFont(new Font("Arial", Font.TRUETYPE_FONT, 22));
        g2d.fillRect(0, 0, WIDTH, HEIGHT);
        // 绘制随机线条
        Random random = new Random();
        for (int i = 0; i < 30; i++) {
            // 起始坐标
            int b_x = random.nextInt(WIDTH);
            int b_y = random.nextInt(HEIGHT);
            // 终点坐标
            int e_x = random.nextInt(b_x + 20);
            int e_y = random.nextInt(b_y + 20);
            // 设置画笔颜色
            g2d.setColor(getColor());
            // 绘制线条
            g2d.drawLine(b_x, b_y, e_x, e_y);
        }
        // 获取随机字符
        String verCode = getString();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < LENGTH; i++) {
            sb.append(verCode.charAt(i) + " "); // 拼接验证码的每一位，空格分开;
        }
        g2d.setColor(Color.black);
        g2d.drawString(sb.toString(), 5, 25); // 绘制验证码
        // 保存验证码 到 session中
        session.setAttribute("verCode", verCode);
        // 释放绘图资源
        g2d.dispose();
        // 产生真实图片并输出页面
        try {
            ImageIO.write(image, "jpeg", response.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 获取随机颜色
     *
     * @return
     */
    public Color getColor() {
        Random random = new Random();
        int r = random.nextInt(256);
        int g = random.nextInt(256);
        int b = random.nextInt(256);
        return new Color(r, g, b);
    }

    /**
     * 获取随机字符串
     *
     * @return
     */
    public String getString() {
        Random rd = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < LENGTH; i++) {
            sb.append(code[rd.nextInt(code.length)]);
        }
        return sb.toString();
    }
}
